/**
 * created by 小卷毛, 2021-07-21
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：收款统计操作相关
 * 作 者：小卷毛  2021-07-21 16:12
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class StatisticFundViewModel : DialogViewModel() {

    val mOnlineList by lazy {
        listOf(
            FilterData("全部", "", "收款方式", true),
            FilterData("微信", "weixin", "微信"),
            FilterData("支付宝", "alipay", "支付宝")
        )
    }

    val mOfflineList by lazy {
        listOf(
            FilterData("全部", "", "收款方式", true),
            FilterData("人工报单", "offline", "人工报单"),
            FilterData("现金", "cash", "现金"),
            FilterData("微信", "weixin", "微信"),
            FilterData("支付宝", "alipay", "支付宝"),
            FilterData("其他", "other", "其他")
        )
    }

    val mMonthList by lazy {
        listOf(
            FilterData("全部", "", "收款方式", true),
            FilterData("记账", "monthly", "记账"),
            FilterData("电子签单", "signature", "电子签单")
        )
    }

    /**
     * 微信订单列表
     */
    fun orderWechat(
        page: Int,
        type: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<Any>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(BaseUrl.stsOrderPay)
            .params(
                getParams(
                    "source" to "online",
                    "type" to type,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map {
                ArrayList<Any>().apply {
                    it.data.data.forEach { item ->
                        // 添加头部
                        add(OrderHeader().apply {
                            orderSN = item.sn
                            hint = "下单时间："
                            time = item.createDate.getTimeString()
                            status_hint = item.statusDescr
                        })

                        // 添加商品
                        addItems(
                            item.orderItems.onEach {
                                it.orderSN = item.sn
                                it.consignee = item.consignee.orEmpty()
                                it.phone = item.phone.orEmpty()
                                it.hopeDate = item.hopeDate.orEmpty()
                                it.reminderDate = item.reminderDate.orEmpty()
                                it.methodDescr = item.methodDescr.orEmpty()
                            }
                        )

                        // 添加底部
                        add(OrderFooter().apply {
                            orderSN = item.sn
                            total_num = item.quantity
                            amount = item.amountPaid.orEmpty()
                        })
                    }
                }
            }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 报单订单列表
     */
    fun orderManual(
        page: Int,
        type: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<Any>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(BaseUrl.stsOrderPay)
            .params(
                getParams(
                    "source" to "offline",
                    "type" to type,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map {
                ArrayList<Any>().apply {
                    it.data.data.forEach { item ->
                        // 添加头部
                        add(OrderHeader().apply {
                            orderSN = item.sn
                            hint = "下单时间："
                            time = item.createDate.getTimeString()
                            status_hint = item.statusDescr
                        })

                        // 添加商品
                        addItems(
                            item.orderItems.onEach {
                                it.orderSN = item.sn
                                it.consignee = item.consignee.orEmpty()
                                it.phone = item.phone.orEmpty()
                            }
                        )

                        // 添加底部
                        add(OrderFooter().apply {
                            orderSN = item.sn
                            total_num = item.quantity
                            amount = item.amountPaid.orEmpty()
                        })
                    }
                }
            }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 记账订单列表
     */
    fun orderBill(
        page: Int,
        type: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<Any>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(BaseUrl.stsOrderPay)
            .params(
                getParams(
                    "source" to "monthly",
                    "type" to type,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map {
                ArrayList<Any>().apply {
                    it.data.data.forEach { item ->
                        // 添加头部
                        add(OrderHeader().apply {
                            orderSN = item.sn
                            hint = "下单时间："
                            time = item.createDate.getTimeString()
                            status_hint = item.statusDescr
                        })

                        // 添加商品
                        addItems(
                            item.orderItems.onEach {
                                it.orderSN = item.sn
                                it.consignee = item.consignee.orEmpty()
                                it.phone = item.phone.orEmpty()
                            }
                        )

                        // 添加底部
                        add(OrderFooter().apply {
                            orderSN = item.sn
                            total_num = item.quantity
                            amount = item.amountPaid.orEmpty()
                        })
                    }
                }
            }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}